package com.guigu.www.erp.dehao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.www.erp.common.pojo.*;
import com.guigu.www.erp.dehao.pojo.User;
import com.guigu.www.erp.dehao.service.DHMManufactureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/DHMManufacture")
public class DHMManufactureController {

    @Autowired
    private DHMManufactureService service;

    @PostMapping("/addDHMManufacture")
    public void insertSelective(@RequestBody MManufacture mManufacture) {

        //获取编号
        Calendar now = Calendar.getInstance();
        String time="301";
        //年
        time+=now.get(Calendar.YEAR);

        //月
        String month = (now.get(Calendar.MONTH) + 1)+"";
        month=month.length()==1?"0"+month : month;
        time+=month;

        //日
        String day = now.get(Calendar.DAY_OF_MONTH)+"";
        day=day.length()==1?"0"+day : day;
        time+=day;

        //查询最大编号的方法
        String s = service.queryMaxManufacture();
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }

        Date date = new Date();
        String s1 = date.toLocaleString();

        mManufacture.setCheckTag("0");
        mManufacture.setManufactureProcedureTag("0");
        mManufacture.setRegisterTime(s1);
        mManufacture.setManufactureId(time);
        service.insertSelective(mManufacture);
        Integer id = mManufacture.getId();//主键自增id

        //循环添加生产工序和物料明细表
        for (MProcedure mProcedure : mManufacture.getMProcedures()) {
            //设置序号
            mProcedure.setParentId(id);
            mProcedure.setId(null);
            service.insertProcedure(mProcedure);
            Integer id1 = mProcedure.getId();//返回主键自增id
            for (MProcedureModule mProcedureModule : mProcedure.getMProcedureModules()) {
                //设置序号
                mProcedureModule.setId(null);
                mProcedureModule.setParentId(id1);
                service.insertModule(mProcedureModule);
            }
        }

        //循环修改生产计划的状态
        String applyIdGroup = mManufacture.getApplyIdGroup();
        String[] split = applyIdGroup.split(",");
        for (int i = 0; i < split.length; i++) {
            MApply mApply = new MApply();
            mApply.setId(Integer.parseInt(split[i]));
            mApply.setManufactureTag("1");
            service.updateMApplyTag(mApply);
        }
    }

    @GetMapping("/byId/{id}")
    public MManufacture selectByPrimaryKey(@PathVariable Integer id) {
        return service.selectByPrimaryKey(id);
    }

    @PutMapping("/up")
    public void updateByPrimaryKeySelective(@RequestBody MManufacture mManufacture) {
        service.updateByPrimaryKeySelective(mManufacture);
    }

    @RequestMapping("/queryByCheckMApply1")
    public PageVo<MApply> queryByCheckMApply1(Integer pageNum, Integer pageSize) {
        return service.queryByCheckMApply1(pageNum, pageSize);
    }

    @GetMapping("/numberManufactureTagGroup0")
    public Integer numberManufactureTagGroup0() {
        return service.numberManufactureTagGroup0();
    }

    @GetMapping("/numberManufactureTag0")
    public Integer numberManufactureTag0() {
        return service.numberManufactureTag0();
    }

    @GetMapping("/DHQueryByProductId")
    public MDesignProcedure DHQueryByProductId(String productId) {
        return service.DHQueryByProductId(productId);
    }

    @GetMapping("/DHQueryByParentIdProcedureDetails")
    public List<MDesignProcedureDetails> queryByParentIdProcedureDetails(String parentId) {
        return service.queryByParentIdProcedureDetails(parentId);
    }

    @GetMapping("/queryByParentIdModule")
    public List<MDesignProcedureModule> queryByParentIdModule(Integer parentId) {
        return service.queryByParentIdModule(parentId);
    }

    @RequestMapping("/queryAllByCheckTag")
    public PageVo<MManufacture> queryAllByCheckTag(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser.getRole().getRoleId()==4){
            return null;
        }
        return service.queryAllByCheckTag(pageNum,pageSize);
    }

    @GetMapping("/queryByParentIdPM")
    public List<MProcedureModule> queryByParentIdPM(Integer parentId) {
        return service.queryByParentIdPM(parentId);
    }


    @RequestMapping("/queryAllMManufacture")
    public PageVo<MManufacture> queryAllMManufacture(Integer pageNum, Integer pageSize, String productName, String startTime, String endTime) {
        return service.queryAllMManufacture(pageNum, pageSize, productName, startTime, endTime);
    }

    @GetMapping("/queryByCheckTag0")
    public Integer queryByCheckTag0() {
        return service.queryByCheckTag0();
    }

    @GetMapping("/queryByCheckTag1")
    public Integer queryByCheckTag1() {
        return service.queryByCheckTag1();
    }

    @GetMapping("/queryByCheckTag2")
    public Integer queryByCheckTag2() {
        return service.queryByCheckTag2();
    }

    @GetMapping("/getNewMaxId")
    public void getNewMaxId(HttpServletResponse response) throws IOException {
        //获取编号
        Calendar now = Calendar.getInstance();
        String time="301";
        //年
        time+=now.get(Calendar.YEAR);

        //月
        String month = (now.get(Calendar.MONTH) + 1)+"";
        month=month.length()==1?"0"+month : month;
        time+=month;

        //日
        String day = now.get(Calendar.DAY_OF_MONTH)+"";
        day=day.length()==1?"0"+day : day;
        time+=day;

        //查询最大编号的方法
        String s = service.queryMaxManufacture();
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }

        response.getWriter().write(time);
    }

    @PostMapping("/insertSPay")
    public void insertSPay(@RequestBody SPay sPay) {
        //获取编号
        Calendar now = Calendar.getInstance();
        String time="402";
        //年
        time+=now.get(Calendar.YEAR);

        //月
        String month = (now.get(Calendar.MONTH) + 1)+"";
        month=month.length()==1?"0"+month : month;
        time+=month;

        //日
        String day = now.get(Calendar.DAY_OF_MONTH)+"";
        day=day.length()==1?"0"+day : day;
        time+=day;

        //查询最大编号的方法
        String s = service.queryMaxSPay();
        if (s==null){
            time+="0001";
        }else {
            Integer i = Integer.parseInt(s.substring(11));
            i++;
            String str=i+"";
            if (str.length()==1){
                str="000"+str;
            }else if (str.length()==2){
                str="00"+str;
            }else if (str.length()==3){
                str="0"+str;
            }
            time+=str;
        }
        Date date = new Date();
        String s1 = date.toLocaleString();

        Integer num = sPay.getId();
        sPay.setId(null);
        sPay.setPayId(time);
        sPay.setCheckTime(new Date().toLocaleString());
        sPay.setRegisterTime(s1);
        sPay.setReason("生产领料");

        service.insertSPay(sPay);

        for (SPayDetails sPayDetail : sPay.getSPayDetails()) {
            sPayDetail.setPayTag("1");
            sPayDetail.setId(null);
            sPayDetail.setAmount(sPayDetail.getAmount()*num);
            sPayDetail.setSubtotal(sPayDetail.getSubtotal()*num);
            sPayDetail.setParentId(sPay.getId());
            service.insertSPayDetails(sPayDetail);
        }

    }

    @GetMapping("/queryModuleAmount")
    public Integer queryModuleAmount(String arr) {
        return service.queryModuleAmount(arr);
    }


    @GetMapping("/queryModuleSubtotal")
    public Integer queryModuleSubtotal(String arr) {
        return service.queryModuleSubtotal(arr);
    }

    @GetMapping("/queryArrModuleDetails")
    public List<MProcedureModule> queryArrModuleDetails(String arr) {
        List<MProcedureModule> mProcedureModules = service.queryArrModuleDetails(arr);
        List<MProcedureModule> mmm = new ArrayList<>();

        for (MProcedureModule mProcedureModule : mProcedureModules) {
            boolean flag = false;
            MProcedureModule pro = new MProcedureModule();
            for (MProcedureModule procedureModule : mmm) {
                if (procedureModule.getProductId().contains(mProcedureModule.getProductId())){
                    pro=procedureModule;
                    flag=true;
                }
            }
            if (flag){
                pro.setAmount(pro.getAmount()+mProcedureModule.getAmount());
                pro.setSubtotal(pro.getSubtotal()+mProcedureModule.getSubtotal());
            }else{
                mmm.add(mProcedureModule);
            }
        }
        return mmm;
    }

    @PutMapping("/updateMProcedureById")
    public void updateMProcedureById(@RequestBody MProcedure mProcedure) {
        service.updateMProcedureById(mProcedure);
    }

}
